#!/usr/bin/env python

import web
from config import settings
from time import gmtime, strftime
import db

render = settings.render

class Item:
    title=''
    content=''
    datetime=''

login = web.form.Form(
    web.form.Textbox('User'),
    web.form.Password('Pwd'),
    web.form.Button('Login'),
    )

class Login():
    
    def GET(self):
        login_form = login()
        return render.index(login_form)
    
    def POST(self):
        login_form = login()
        if login_form.validates():
            if login_form.d.get('User') == 'root' and login_form.d.get('Pwd')== 'root':
                return web.seeother('/login')
        raise web.seeother('/')


Newblog = web.form.Form(  
                         web.form.Textbox('Title',  
                         web.form.notnull,  
                         size=30,  
                         description='Title: '),  
                         web.form.Textarea('Content',  
                         web.form.notnull,  
                         rows=30,  
                         cols=80,  
                         description='Content: '),  
                         web.form.Button('Commit'),
                         )  

class Index():
    
    def GET(self):
        content = []
        myblog = Newblog()
        rows = db.get_content()
        for item in rows:
            ele = Item()
            ele.title = item[0]
            ele.content = item[1]
            content.append(ele)
        return render.Index(content, myblog)
    
    def POST(self):
        myblog = Newblog()
        if not myblog.validates():
            raise web.seeother('/')
        #print myblog.d.get('Title'),myblog.d.get('Content')
        db.new_post(myblog.d.get('Title'), myblog.d.get('Content'))
        return web.seeother('/')

